<?php


namespace addons\video\model;

/**
 * Description of live
 * 直播
 * @author zmh
 */
class GiftLog extends \web\common\model\BaseModel{
    
    protected function _initialize(){
        $this->tableName = 'live_gift_log';
    }

    public function addLog($user_id, $gift_id, $anchor_id, $count = 1) {
        if ($user_id <= 0 || $gift_id <= 0 || $anchor_id <= 0)
            return false;
        $data['user_id'] = $user_id;
        $data['count'] = $count;
        $data['gift_id'] = $gift_id;
        $data['anchor_id'] = $anchor_id;
        $data['create_at'] = date('Y-m-d H:i:s');
        return $this->insert($data);
    }


    /**
     * 主播礼物统计
     */
    public function anchorTotal($anchor_id){
        return $this->alias('gl')->where('anchor_id',$anchor_id)->join('live_gift g','g.id=gl.gift_id','left')->sum('g.gift_amount*gl.count');
    }


    /**
     * 主播礼物统计
     */
    public function anchorGiftRank($anchor_id,$user_id,$page=0,$rows=10){
        return $this->alias('gl')->field('sum(gift_amount * gl.count) sum_price,gl.user_id,m.head_img,m.username,lml.level_name,lml.level_rank,if(la.member_id>=1,"1","0") is_attention')->where('gl.anchor_id',$anchor_id)->join('live_gift g','g.id=gl.gift_id','left')->join('member_account m','m.id=gl.user_id','left')->join('live_member_level lml','lml.user_id=gl.user_id','left')->join('(select member_id from tp_live_member_attention where  user_id = '.$user_id.') la','la.member_id=gl.user_id','left')->group('user_id')->order('sum_price desc')->limit($page*$rows,$rows)->select();
    }


    /**
     * 主播礼物统计
     */
    public function giftRank($page=0,$rows=10){
        return $this->alias('gl')->field('sum(gift_amount * gl.count) sum_price,gl.user_id,m.head_img,m.username,lml.level_name,lml.level_rank')->join('live_gift g','g.id=gl.gift_id','left')->join('member_account m','m.id=gl.user_id','left')->join('live_member_level lml','lml.user_id=gl.user_id','left')->group('user_id')->order('sum_price desc')->limit($page*$rows,$rows)->select();

    }


    /**
     * 主播礼物统计
     */
    public function anchorSwooleGiftRank($anchor_id,$page=0){
        return $this->alias('gl')->field('sum(gift_amount * gl.count) sum_price,m.head_img')->where('anchor_id',$anchor_id)->join('live_gift g','g.id=gl.gift_id','left')->join('member_account m','m.id=gl.user_id','left')->group('user_id')->order('sum_price desc')->limit($page*10,10)->select();
    }

}
